package com.hot12345.springmvc.admin;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.hot12345.common.spring.SpringAbstractController;
import com.hot12345.dao.ItemDaoImpl;
import com.hot12345.dao.ItemSiteDaoImpl;
import com.hot12345.entity.Item;
import com.hot12345.entity.ItemSite;

@Controller
@RequestMapping("/admin/item/site")
public class ItemSiteController extends SpringAbstractController {

	@RequestMapping("/index")
	public ModelAndView index(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap,
			@RequestParam(value="itemID", required = false) Integer itemID) throws Exception {
		
		ItemSiteDaoImpl itDao = ItemSiteDaoImpl.getInstance();
		pageBefore(request, modelMap);
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("state_gte", 0);
		params.put("itemID", itemID);
		
				
		List<ItemSite> linkList = itDao.findByParamsPage(params, getStartNo(modelMap), getPageSize(modelMap));
		long totalNum = itDao.findCount(params);
		
		pageAfter(request, totalNum, modelMap);
		
		modelMap.put("linkList", linkList);
		
		
		ItemDaoImpl itemDao = ItemDaoImpl.getInstance();
		List<Item> itemList = itemDao.getItemAll();
		modelMap.put("itemList", itemList);
		modelMap.put("currID", itemID);
		
		return new ModelAndView("admin/item/site/itemSiteList");
	}
	
	@RequestMapping("/insert")
	public void insert(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap,
			@RequestParam("itemID") Integer itemID,
			@RequestParam("siteName") String siteName,
			@RequestParam("sort") Integer sort,
			@RequestParam("url") String url) throws Exception {
		ItemSite itemSite = new ItemSite();
		itemSite.setItemID(itemID);
		itemSite.setSiteName(siteName);
		itemSite.setSort(sort);
		itemSite.setUrl(url);
		itemSite.setState(1);
		
		ItemSiteDaoImpl itDao = ItemSiteDaoImpl.getInstance();
		itDao.insert(itemSite);
		
		
		response.sendRedirect("/admin/item/site/index");
	}
	
	@RequestMapping("/add")
	public ModelAndView add(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap) throws Exception {
				
		return new ModelAndView("admin/item/site/itemSiteAdd");
	}
	
	@RequestMapping(value="/edit", method = RequestMethod.GET)
	public ModelAndView edit(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap,
			int id) throws Exception {
		
		ItemSiteDaoImpl itDao = ItemSiteDaoImpl.getInstance();
		ItemSite itemSite = itDao.findByPrimaryKey(id);
		modelMap.put("itemSite", itemSite);
		
		return new ModelAndView("admin/item/site/itemSiteEdit");
	}
	
	@RequestMapping(value="/update", method = RequestMethod.POST)
	public void update(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap,
			@RequestParam("id") Integer id,
			@RequestParam("itemID") Integer itemID,
			@RequestParam("siteName") String siteName,
			@RequestParam("sort") Integer sort,
			@RequestParam("url") String url) throws Exception {
		
		ItemSiteDaoImpl itDao = ItemSiteDaoImpl.getInstance();
		ItemSite itemSite = new ItemSite();
		itemSite.setId(id);
		itemSite.setItemID(itemID);
		itemSite.setSiteName(siteName);
		itemSite.setSort(sort);
		itemSite.setUrl(url);
		itemSite.setState(1);
		itDao.update(itemSite);
		
		response.sendRedirect("/admin/item/site/index");
	}
	
	/**
	 * 冻结
	 *	
	 * @param request
	 * @param response
	 * @param modelMap
	 * @param id
	 * @throws Exception
	 * @author wuzongbao
	 * @date 2013-3-3 下午11:48:23
	 * @version V1.0
	 */

	@RequestMapping(value="/freeze", method = RequestMethod.GET)
	public void freeze(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap,
			int id) throws Exception {
		
		ItemSiteDaoImpl itDao = ItemSiteDaoImpl.getInstance();
		ItemSite itemSite = itDao.findByPrimaryKey(id);
		itemSite.setState(0);
		itDao.update(itemSite);
		
		response.sendRedirect("/admin/item/site/index");
	}
	
	/**
	 * 恢复正常状态
	 *	
	 * @param request
	 * @param response
	 * @param modelMap
	 * @param id
	 * @throws Exception
	 * @author wuzongbao
	 * @date 2013-3-3 下午11:48:43
	 * @version V1.0
	 */
	@RequestMapping(value="/restore", method = RequestMethod.GET)
	public void restore(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap,
			int id) throws Exception {
		
		ItemSiteDaoImpl itDao = ItemSiteDaoImpl.getInstance();
		ItemSite itemSite = itDao.findByPrimaryKey(id);
		itemSite.setState(1);
		itDao.update(itemSite);
		
		response.sendRedirect("/admin/item/site/index");
	}
}
